package com.lez.flowpractice2.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase

@Database(entities = [User::class],version = 1, exportSchema = true)
abstract class AppDatabase:RoomDatabase() {

    abstract fun userDao() : UserDao

    //伴生对象的主要作用就是为其所在的外部类模拟静态成员,类似static
    companion object{

        private var instance:AppDatabase? = null

        fun getInstance(context: Context):AppDatabase{
            return instance ?: synchronized(this){
                Room.databaseBuilder(context,AppDatabase::class.java,"my_user.db")
                    .build().also {
                    instance = it
                }
            }
        }
    }
}